package edu.wuster.blog.service.impl;

import edu.wuster.blog.entity.Label;
import edu.wuster.blog.exception.MyException;
import edu.wuster.blog.mapper.BlogpartMapper;
import edu.wuster.blog.mapper.LabelMapper;
import edu.wuster.blog.service.LabelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;



@Slf4j
@Service
public class LabelServiceImpl implements LabelService{

    @Autowired
    private LabelMapper labelMapper;

    @Autowired
    private BlogpartMapper blogpartMapper;

    @Override
    public void addLabel(Label label){
        log.info("添加标签");
        if (labelMapper.selectAdded(label) != null){
            log.info("该标签已存在于博客");
            throw new MyException("标签添加失败");
        }
        if (blogpartMapper.selectById(label.getBlogId())==null) {
            log.info("添加标签的博客不存在");
            throw new MyException("添加标签的博客不存在");
        }
        labelMapper.addLabel(label);
    }

    @Override
    public void deleteLabel(Integer id){
        labelMapper.deleteLabel(id);
    }

    @Override
    public List<Label> selectByBlogId(Integer blogId) {

        if (blogpartMapper.selectById(blogId)==null) {
            log.info("查看标签的博客不存在");
            throw new MyException("查看标签的博客不存在");
        }
        log.info("查看博客（id：{}）的标签", blogId);
        return labelMapper.selectByBlogId(blogId);
    }

}
